package com.fingard.dsp.bank.directbank.bea01;

import com.fingard.dsp.bank.directbank.bankInterface.IDeposit;
import com.fingard.dsp.bank.reqrespobj.deposit.Req7507DepQry;
import com.fingard.dsp.bank.reqrespobj.deposit.RetAts7507DepQry;
import com.fingard.dsp.bank.reqrespobj.deposit.detail.RetAts7507Detail;
import com.fingard.text.StringHelper;
import com.fingard.util.DateHelper;
import com.fingard.xml.XElement;
import com.fingard.xml.XmlTextReader;
import com.fingard.xml.XmlTextWriter;

public class BEA01Deposit extends BEA01Base implements IDeposit {

    @Override
    public RetAts7507DepQry d_7507_DepQry(Req7507DepQry p_req7507) throws Exception {
        RetAts7507DepQry retAts = new RetAts7507DepQry(p_req7507.reqHeader);
        retAts.writeStart();
        String tmpRespCode = "";
        String tmpRespInfo = "";
        try {
            BEA01Header tmpHeader = new BEA01Header();
            tmpHeader.SERVICE_CODE = "2205";
            tmpHeader.TRAN_REF_NO = getQryTransID();
            XmlTextWriter xmlWriter = new XmlTextWriter();
            writeCommonHeader(xmlWriter, tmpHeader);
            //账号-定期编号
            if(p_req7507.actNum.contains("-")){
                xmlWriter.writeElementString("ACCT_NO", p_req7507.actNum.substring(0,p_req7507.actNum.indexOf("-")));
                xmlWriter.writeElementString("FXD_NO", p_req7507.actNum.substring(p_req7507.actNum.indexOf("-")+1));
            }else {
                xmlWriter.writeElementString("ACCT_NO", p_req7507.actNum);
                xmlWriter.writeElementString("FXD_NO", "");
            }
            xmlWriter.writeElementString("reqTime", "");
            xmlWriter.writeElementString("CHECKPS", "");
            xmlWriter.writeEndDocument();

            String[] retStrArr = sendToBank(xmlWriter.toXmlString());

            //WriteBankLogLn("请求银行："+xmlWriter.toXmlString());
            //String[] retStrArr=new String[]{"",FileHelper.readFileText("D:\\xmlRspTest\\rsp.txt","utf-8")};
            //WriteBankLogLn("银行响应："+retStrArr[1]);

            if (retStrArr[0].length() == 0) {
                StringBuilder tmpSbMsg = new StringBuilder();
                XmlTextReader tmpBkReader = loadBankRespXmlReader(retStrArr[1], tmpSbMsg);
                if (tmpBkReader != null) {
                    String tmpRetCode = tmpBkReader.getFirstText("/BCDL/opRep/retCode");
                    String tmpRetMsg=tmpBkReader.getFirstText("/BCDL/opRep/errMsg");
                    if ("000000".equals(tmpRetCode)) {
                        while (tmpBkReader.read("/BCDL/opRep/opResult")){
                            XElement readXElement=tmpBkReader.readedXElement;
                            RetAts7507Detail retAts7507Detail = new RetAts7507Detail();
                            retAts7507Detail.actNum=p_req7507.actNum;//银行账号
                            retAts7507Detail.actArea=p_req7507.actArea;//账号地区区域码
                            retAts7507Detail.depositCode=readXElement.getSingleTextNotNull("FXD_NO").trim();//定存单号
                            retAts7507Detail.startDate=DateHelper.convertD8toD10(readXElement.getSingleTextNotNull("FXD_SDAT").trim());//起始日期
                            retAts7507Detail.endDate=DateHelper.convertD8toD10(readXElement.getSingleTextNotNull("FXD_EDAT").trim());//截止日期
                            retAts7507Detail.valueDate=DateHelper.convertD8toD10(readXElement.getSingleTextNotNull("FXD_SDAT").trim());//起息日期
                            String per=readXElement.getSingleTextNotNull("FXD_PER").trim();//定期存期1、2、3、5、6、7
                            String unit=readXElement.getSingleTextNotNull("FXD_UNIT").trim();//存期单位Y:年、M:月、D:日
                            String terms="";
                            if("D".equals(unit)){
                                switch (Integer.parseInt(per)){
                                    case 1:terms="0";break;
                                    case 7:terms="1";break;
                                    default:break;
                                }
                            }else if("M".equals(unit)){
                                switch (Integer.parseInt(per)){
                                    case 1:terms="2";break;
                                    case 3:terms="3";break;
                                    case 6:terms="4";break;
                                    default:break;
                                }
                            }else if("Y".equals(unit)){
                                switch (Integer.parseInt(per)){
                                    case 1:terms="5";break;
                                    case 2:terms="6";break;
                                    case 3:terms="7";break;
                                    case 5:terms="8";break;
                                    default:break;
                                }
                            }
                            //其它
                            if(StringHelper.isNullOrEmpty(terms)){
                                terms="9";
                            }
                            retAts7507Detail.terms=terms;//存款期限 (七天`1,一个月`2,三个月`3,六个月`4,一年`5,二年`6,三年`7,五年`8,其它`9)
                            retAts7507Detail.currencyID=convertCurToATS(readXElement.getSingleTextNotNull("CCY"));//币种
                            retAts7507Detail.noteMoney=readXElement.getSingleTextNotNull("FXD_AMT1").trim();//金额
                            retAts7507Detail.rate=readXElement.getSingleTextNotNull("FXD_RATE").trim();//利率(%)
                            retAts7507Detail.interestMoney=readXElement.getSingleTextNotNull("FXD_INT").trim();//利息
                            retAts7507Detail.depositStates="";//存单状态 (正常`1,冻结`2,未启用`3,已退票`4,销户`5,挂失`7,损益`6,作废`9)
                            retAts7507Detail.dumpFlag="";//到期处理方式 (不转存`1,本息转存`2,本金转存，利息转活期`3)
                            retAts7507Detail.drawDate="";//上划日期
                            retAts.writeRetDetail(retAts7507Detail);
                        }
                    }else {
                        tmpRespCode = "-1";
                        tmpRespInfo = tmpRetMsg;
                    }
                }else {
                    tmpRespCode = "-1";
                    tmpRespInfo = tmpSbMsg.toString();
                }
            }else {
                tmpRespCode = "-1";
                tmpRespInfo = retStrArr[1];
            }
        } catch (Exception e) {
            tmpRespCode = "-1";
            tmpRespInfo = e.getMessage();
            e.printStackTrace();
        }
        if ("-1".equals(tmpRespCode) && !retAts.hasWrittenRespCode) {
            retAts = new RetAts7507DepQry(p_req7507.reqHeader);
            retAts.writeStart();//写返回ATS的统一头部信息
            retAts.writeRespCode(tmpRespCode);
            retAts.writeRespInfo(tmpRespInfo);
        } else if (retAts.getWrittenRdCount() == 0) {
            retAts.writeRespCode("0");
            retAts.writeRespInfo("查无存单");
        }
        retAts.writeEnd();
        return retAts;
    }
}
